package com.first.first.service;

import com.first.first.pojo.User;
import com.first.first.pojo.dto.UserDto;
import com.first.first.pojo.dto.UserEditDto;
import com.first.first.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService{
    @Autowired
    UserRepository userRepository;

    @Override
    public User addUser(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user, userPojo);
        return userRepository.save(userPojo);
    }

    @Override
    public User getUser(Integer id) {
        return userRepository.findById(id).orElseThrow(() -> new RuntimeException("用户不存在"));
    }

    @Override
    public void updateUser(UserEditDto user) {
        User userUpdate = new User();
        BeanUtils.copyProperties(user, userUpdate);
        userRepository.save(userUpdate);
    }

    @Override
    public void deleteUser(Integer id) {
        userRepository.deleteById(id);
    }
}
